package blog;

import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;

import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

import static java.lang.Thread.sleep;

/**
 * @author 23050
 * @version 1.0
 * @description: 正常测试用例
 * @date 2024/1/9 10:32
 */
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class BlogCases extends InitAndEnd{



    /**
     * @author 23050
     * @description:  输入正确的账号，密码登录成功
     * @date: 2024/1/9 10:33
     * @return null
     */
    @Order(1)
    @ParameterizedTest
    @CsvFileSource(resources = "loginSuccess.csv")
    void loginSuccess(String username,String password,String code,String blog_list_url) {
        //打开博客登录页面
        webDriver.get("http://127.0.0.1:8080/login.html");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //输入账号 zhangsan
        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //输入密码 123
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //输入验证码
        webDriver.findElement(By.cssSelector("#codeinput")).sendKeys(code);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //点击提交按钮
        webDriver.findElement(By.cssSelector("#submit")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //页面跳转到对应的列表页
        String cur_url = webDriver.getCurrentUrl();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //获取当前页面的url
        //如果url==http://127.0.0.1:8080/admin_blog_list_all.html
        Assertions.assertEquals(blog_list_url,cur_url);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //跳转到个人中心文章列表页
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(4)")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(7)")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //列表页展示用户信息是zhangsan
        String cur_admin = webDriver.findElement(By.cssSelector("#username")).getText();
        Assertions.assertEquals(username,cur_admin);





    }


    /**
     * @author 23050
     * @description:  校验博客数量
     * @date: 2024/1/9 11:11
     * @return void
     */
    @Order(2)
    @Test
    void BlogList(){
        //打开博客列表页
        webDriver.get("http://127.0.0.1:8080/myblog_list.html");
        //获取页面上所有博客标题，对应的元素
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        int title_num = webDriver.findElements(By.cssSelector(".title")).size();
        //如果元素为0，测试通过
        Assertions.assertNotEquals(0,title_num);



    }


    public static Stream<Arguments> Generator() {
        return Stream.of(Arguments.arguments("http://127.0.0.1:8080/blog_content.html?id=",
                "博客正文",
                "自动化测试"));
    }

    /**
     * @author 23050
     * @description:  博客详情页的校验
     * @date: 2024/1/9 11:21
     * @return null
     * url
     * 博客标题
     * 页面的title是否对应
     */
    @Order(3)
    @ParameterizedTest
    @MethodSource("Generator")
    void BlogDetail(String expected_url,String expected_title,String expected_blog_title){
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        webDriver.findElement(By.xpath("/html/body/div[2]/div[2]/div/a[1]")).click();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        //获取当前页面url
        String cur_url = webDriver.getCurrentUrl();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        //获取当前页面title
        String cur_title = webDriver.getTitle();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        //获取博客标题
        String blog_title = webDriver.findElement(By.cssSelector("#title")).getText();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);

//        Assertions.assertEquals(expected_url,cur_url);
        Assertions.assertEquals(expected_title,cur_title);
        Assertions.assertEquals(expected_blog_title,blog_title);
        if (cur_url.contains(expected_url)){
            System.out.println("测试通过");
        }

    }


    /**
     * @author 23050
     * @description:  发布博客并审核
     * @date: 2024/1/9 15:45
     * @return void
     */
    @Order(4)
    @Test
    void EditBlog() throws InterruptedException {
        webDriver.navigate().back();
        //找到写博客按钮，找到输入框
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);

//        sleep(3000);
        //通过js将标题进行输入
        ((JavascriptExecutor)webDriver).executeScript("document.getElementById(\"title\").value=\"自动化测试\"");
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
//        sleep(3000);
        //点击发布
        webDriver.findElement(By.cssSelector("body > div.blog-edit-container > div.title > button:nth-child(3)")).click();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        webDriver.switchTo().alert().accept();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        webDriver.switchTo().alert().dismiss();
//        sleep(3000);
        //获取当前的url
        String cur_url = webDriver.getCurrentUrl();
        Assertions.assertEquals("http://127.0.0.1:8080/myblog_list.html",cur_url);


        //审核
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        webDriver.get("http://127.0.0.1:8080/admin_review_list.html");
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        webDriver.findElement(By.cssSelector("#artDiv > div > a:nth-child(5)")).click();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        webDriver.switchTo().alert().accept();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        webDriver.switchTo().alert().accept();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);


    }



    /**
     * @author 23050
     * @description:  检验刚才发布的博客
     * @date: 2024/1/9 16:01
     * @return void
     */
    @Test
    @Order(5)
    void BlogInfoChecked(){
        webDriver.get("http://127.0.0.1:8080/myblog_list.html");
        //获取第一篇博客的标题
        String first_blog_title = webDriver.findElement(By.cssSelector("#artDiv > div:nth-child(1) > div.title")).getText();
        String first_blot_time = webDriver.findElement(By.cssSelector("#artDiv > div:nth-child(1) > div.date")).getText();
        Assertions.assertEquals("自动化测试",first_blog_title);
        //如果时间是2024-01-09 15:49:23发布的，测试通过
        if (first_blot_time.contains("2024-01-09")){
            System.out.println("测试通过");
        }else{
            System.out.println("当前时间:"+first_blot_time);
            System.out.println("测试不通过");
        }

    }

    @Order(6)
    @Test
    void deleteBlog(){
        //打开博客列表页面
        webDriver.get("http://127.0.0.1:8080/myblog_list.html");
        //校验第一篇博客的标题
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        String text = webDriver.findElement(By.cssSelector("#artDiv > div:nth-child(1) > div.title")).getText();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        Assertions.assertEquals("自动化测试",text);
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        //点击查看删除按钮
        webDriver.findElement(By.cssSelector("#artDiv > div:nth-child(1) > a:nth-child(6)")).click();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        //弹窗确认
        webDriver.switchTo().alert().accept();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        webDriver.switchTo().alert().accept();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
    }


    @Order(7)
    @Test
    void logout() {
        //点击注销按钮
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(7)")).click();
        webDriver.switchTo().alert().accept();
        //校验url(http://127.0.0.1:8080/login.html)
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        String cur_url = webDriver.getCurrentUrl();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        Assertions.assertEquals("http://127.0.0.1:8080/login.html",cur_url);
        //校验提交按钮
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        WebElement webElement = webDriver.findElement(By.cssSelector("#submit"));
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        Assertions.assertNotNull(webElement);
        System.out.println("logout测试通过");


    }


















}


















